package com.wk.controller.student;

import com.sun.mail.imap.protocol.ID;
import com.wk.pojo.*;
import com.wk.service.Examservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: thesis
 * @description: 学生的考试类
 * @author: 王侃
 * @create: 2020-02-29 08:42
 **/
@Controller
@RequestMapping("/stu")
public class Stuexamcontroller {

    @Autowired
    Examservice examservice;

    //跳转到学生考试总览界面
    @RequestMapping("/exam/overview")
    public String exam_overview(HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Final_examination> examinations=examservice.selectbyclass_id_exam(user.getClass_id());
        model.addAttribute("exams",examinations);
        return "student/exam/exam_overview";
    }

    //跳转到学生考试界面
    @RequestMapping("/exam/processing/{id}")
    public String exam_processing(@PathVariable("id")Integer id,HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        Integer flag=examservice.selectbyid_deadline(id);
        //flag为现在时间到截至时间还剩多少
        Final_stu_results results=examservice.selectbystu_results(user.getId(),id);
        //results是说明学生是否已经作答
        if (flag<=0)
        {
            model.addAttribute("main_id",id);
            model.addAttribute("msg","超过截至时间，不能作答");
            List<Final_examination> examinations=examservice.selectbyclass_id_exam(user.getClass_id());
            model.addAttribute("exams",examinations);
            return "student/exam/exam_overview";
        }
        else
        {
            if (results!=null)
            {
                model.addAttribute("main_id",id);
                model.addAttribute("msg","你已经作答过，重复作答无效哦");
                List<Final_examination> examinations=examservice.selectbyclass_id_exam(user.getClass_id());
                model.addAttribute("exams",examinations);
                return "student/exam/exam_overview";

            }
            //查找出来然后存入数据库
            Final_examination examination=examservice.selectbytopicid(id);
            Final_stu_results stu_results=new Final_stu_results();
            stu_results.setTea_id(examination.getUser_id());
            stu_results.setClass_id(examination.getClass_id());
            stu_results.setExam_id(examination.getId());
            stu_results.setStu_id(user.getId());
            stu_results.setStu_name(user.getUsername());
            stu_results.setStatus(0);
            examservice.insertintostu_results(stu_results);
            List<Final_content> contents=examservice.selectbyexam_content(id);

            //还要获得截至时间
            model.addAttribute("deadline_time",flag-2);

            model.addAttribute("exam",examination);
            model.addAttribute("contents",contents);
            //别忘了传试卷id
            model.addAttribute("result_id",id);
            return "student/exam/exam_processing";
        }
    }

    //进行学生考试习题提交
    @RequestMapping("/exam/process/submit")
    public String process_submit(Integer result_id,@RequestParam Map<String,String> map,Model model,HttpSession session)
     {
        User user=(User)session.getAttribute("loginuser");

        Set<String> set=map.keySet();//用来获取题目的id号,所以id号为String
        Iterator<String> iterator=set.iterator();//必需的
        //通过学生的id和试卷id唯一锁定作答情况id
        Final_stu_results results=examservice.selectbystu_results(user.getId(),result_id);
        Integer flag=0;
        while (iterator.hasNext())
        {
            //这里要分开，不能把result_id加入进去
            if (flag!=0)
            {
                Final_stu_content stu_content=new Final_stu_content();
                String key=iterator.next();
                Final_content content=examservice.selectbyidcontent_string(key);
                //在这里要分情况，总共有四种题型
                //1.选择题
                if(content.getType_id()==1||content.getType_id()==2)
                {
                    stu_content.setAnswer_a(content.getAnswer_a());
                    stu_content.setAnswer_b(content.getAnswer_b());
                    stu_content.setAnswer_c(content.getAnswer_c());
                    stu_content.setAnswer_d(content.getAnswer_d());
                    stu_content.setSubject(content.getSubject());
                    stu_content.setType_id(content.getType_id());
                    stu_content.setResult_id(results.getId());
                    stu_content.setMyanswer(map.get(key));
                }
                //2.问答题
                else if(content.getType_id()==3)
                {
                    stu_content.setSubject(content.getSubject());
                    stu_content.setType_id(content.getType_id());
                    stu_content.setResult_id(results.getId());
                    stu_content.setMyanswer(map.get(key));
                }
                //3.图文题
                else if (content.getType_id()==4)
                {
                    stu_content.setSubject(content.getSubject());
                    stu_content.setType_id(content.getType_id());
                    stu_content.setResult_id(results.getId());
                    stu_content.setMyanswer(map.get(key));
                    stu_content.setMyimage(content.getImage());
                }
                //加入数据库
                examservice.insertintostu_content(stu_content);
            }
            else {
                String key=iterator.next();
            }
            flag++;
        }
        return "redirect:/";
    }

    //跳转到我的成绩记录页面
    @RequestMapping("/exam/mydegree/list")
    public String mydegree_list(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Final_academic> list=examservice.selectbystu_id_academic(user.getId());
        model.addAttribute("list",list);
        return "student/exam/exam_mydegree";
    }
}
